package cn.gmph.blog.service.post;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import cn.gmph.blog.service.post.dao.CommentsDAO;
import cn.gmph.blog.service.post.entity.CommentsDO;
import cn.gmph.blog.web.dto.CommentQueryDTO;
import cn.gmph.blog.web.dto.QueryDTO;
import cn.gmph.core.base.BaseService;

/**
 * 评论信息
 * 
 * @author sxfei 2016-07-16
 */
@Service
public class CommentsService extends BaseService {
    @Autowired
    private CommentsDAO commentsDAO;

    public CommentsDO getByPrimaryKey(Long id) {
        QueryDTO dto = new QueryDTO();
        dto.setId(id);
        return commentsDAO.selectByPrimaryKey(dto);
    }

    public List<Long> getLastComments() {
        QueryDTO dto = new QueryDTO();
        return commentsDAO.selectLastComments(dto);
    }

    public List<CommentsDO> getByIds(List<Long> idList) {
        List<CommentsDO> result = Lists.newArrayList();
        StringBuffer ids = new StringBuffer();
        int i = 0;
        for (Long id : idList) {
            if (i > 0) {
                ids.append(",");
            }
            ids.append(id);
            i++;
        }
        if (Strings.isNullOrEmpty(ids.toString())) {
            return result;
        }
        return commentsDAO.selectByIds(ids.toString());
    }

    public List<Long> getAllLastComments() {
        QueryDTO dto = new QueryDTO();
        return commentsDAO.selectAllLastComments(dto);
    }

    public List<Long> selectForPage(CommentQueryDTO dto) {
        List<String> idList = dto.getStatusList();
        StringBuffer ids = new StringBuffer();
        int i = 0;
        for (String id : idList) {
            if (i > 0) {
                ids.append(",");
            }
            ids.append("'").append(id).append("'");
            i++;
        }
        if (!Strings.isNullOrEmpty(ids.toString())) {
            dto.setStatuses(ids.toString());
        }
        return commentsDAO.selectForPage(dto);
    }
}
